package com.framework.admin.controller;

import com.framework.admin.model.SysMenu;
import com.framework.admin.service.IMenuService;
import com.framework.admin.service.IRoleService;
import com.framework.common.domain.ParamPage;
import com.framework.common.domain.ResultData;
import com.framework.common.vo.TreeNode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping( "/menu")
@Api(value = "menu", tags = "菜单管理")
public class MenuController {
    @Autowired
    private IMenuService menuService;
    @Autowired
    IRoleService roleService;

    /**
     * 获取菜单列表
     */
    @ResponseBody
    @ApiOperation(value = "获取菜单列表")
    @GetMapping(value = "/list")
    public ResultData<Page<SysMenu>> list(SysMenu menu, ParamPage page) {
        PageRequest request = PageRequest.of(page.getPageNo(), page.getPageSize());
        Page<SysMenu> tree = menuService.getMenusList(menu, request);
        return ResultData.data(tree);
    }


    /**
     * 获取单个菜单详情
     */
    @ResponseBody
    @ApiOperation(value = "获取菜单bycode")
    @GetMapping(value = "/getByCode")
    public ResultData<SysMenu> getByCode(SysMenu menu) {
        SysMenu data = menuService.getByCode(menu);
        return ResultData.data(data);
    }


    /**
     * 获取全部菜单列表
     * 用于下拉选择框
     */
    @ResponseBody
    @ApiOperation(value = "获取菜单列表")
    @GetMapping(value = "/tree")
    public ResultData<List<TreeNode<SysMenu>>> tree(String type) {
        List<TreeNode<SysMenu>> tree = menuService.getMenuTree(type);
        return ResultData.data(tree);
    }

    /**
     * 保存菜单
     */
    @ResponseBody
    @ApiOperation(value = "保存菜单")
    @PostMapping(value = "/save")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<String> save(@RequestBody SysMenu menu) {
        String id = menuService.save(menu);
        return ResultData.success(id);
    }

    /**
     * 生成按钮菜单
     */
    @ResponseBody
    @ApiOperation(value = "生成按钮菜单")
    @PostMapping(value = "/btn/{id}")
    public ResultData<String> createBtnMenu(@PathVariable("id") String id) {
        menuService.createBtnMenu(id);
        return ResultData.success();
    }


    /**
     * 删除角色
     */
    @ResponseBody
    @ApiOperation(value = "删除菜单")
    @PostMapping(value = "/del")
    @PreAuthorize("hasAuthority('100')")
    public ResultData<String> del(@RequestBody List<String> ids) {
        menuService.delete(ids);
        return ResultData.success();
    }
}
